package com.xqls.biz.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import com.xqls.bean.RegisterBean;
import com.xqls.biz.RegisterBiz;
import com.xqls.service.RegisterService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class RegisterBizImpl implements RegisterBiz{

	@Autowired private RegisterService service;
	
	@Autowired TransactionTemplate transactionTemplate;
	
	private static Logger logger = Logger.getLogger(RegisterBiz.class);


	@Override
	public CommonResult getRegisByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		CommonResult result = new CommonResult();
		try {
			commonPage.setPageData(service.queryByMap(commonPage.pageToMap()));
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("registerList",commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("register/registerListData.vm",showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("分页条件查询取样异常",e);
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> queryByMap(Map<String, Object> map) {
			List<Map<String, Object>> beanMap = null;
			try {
				beanMap = service.queryByMap(map);
			} catch (Exception e) {
				logger.error("根据map查询取样异常",e);
			}
			return beanMap;
	}

	@Override
	public CommonResult queryByKey(Integer key) {
		CommonResult commonResult=new CommonResult();
		RegisterBean bean = null;
		try {
			bean = service.queryByKey(key);
			commonResult.setData(bean);
		} catch (Exception e) {
			logger.error("根据主键查询取样异常",e);
		}
		return commonResult;
	}

	@Override
	public CommonResult removeList(List<Integer> keys) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			if(keys.size()>0){
				success = service.deleteList(keys);
			}
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("批量删除异常",e);
		}
		return result;
	}

	
	@Override
	public Object editsave(RegisterBean bean) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			if(bean != null){
				RegisterBean registerBean=service.queryByKey(bean.getSamplingId());
				if(registerBean != null){
					success = service.update(bean);
				}else{
					success = service.add(bean);
				}
				result.setIsSuccess(success);
			}
		} catch (Exception e) {
			logger.error("编辑取样异常",e);
		}
		return result;
	}

	@Override
	public Object editc(RegisterBean bean) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			if(bean != null){
				RegisterBean registerBean=service.queryByKey(bean.getSamplingId());
				if(registerBean != null){
					success = service.update(bean);
				}else{
					success = service.add(bean);
				}
				result.setIsSuccess(success);
			}
		} catch (Exception e) {
			logger.error("编辑取样异常",e);
		}
		return result;
	}
}
